package com.example.experiments.filebrowser.views;

import java.io.File;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.progress.UIJob;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.ITreePathLabelProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.SWT;
import swing2swt.layout.FlowLayout;
import org.eclipse.swt.layout.FillLayout;

import com.example.experiments.filebrowser.MyTableContentProvider;
import com.example.experiments.filebrowser.MyTreeLabelProvider;

public class FileBrowserView extends ViewPart {

	private TreeViewer treeViewer;
	private File[] roots;
	
	public FileBrowserView() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		treeViewer = new TreeViewer(container, SWT.FILL);
		Tree tree = treeViewer.getTree();
		treeViewer.setContentProvider(new MyTableContentProvider());
		treeViewer.setLabelProvider( new MyTreeLabelProvider());
		treeViewer.setAutoExpandLevel(1);
//		treeViewer.addSelectionChangedListener(listener)
		loadFiles();
	}

	private void loadFiles() {
		Job job = new Job("Load Files") {
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				roots = File.listRoots();
				System.out.println("Files loaded");
				displayTree();
				return Status.OK_STATUS;
			}

			private void displayTree() {
				UIJob uijob = new UIJob("Display Tree") {
					
					@Override
					public IStatus runInUIThread(IProgressMonitor monitor) {
						treeViewer.setInput( roots);
						System.out.println("Files displayed");
						return Status.OK_STATUS;
					}
				};
				uijob.schedule();
				
			}
		};
		job.schedule();
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

}
